<script>
import { cloneDeep } from 'lodash-es';
import { getToken } from '@/utils/auth';
import { getNewsInfor, addNewsInfor, editZx, removeZx } from '@/api/index.js';
export default {
  name: 'ZixunWeihu',
  data() {
    return {
      contBoxHeight: 0,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 用户表格数据
      tableList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        informationName: '',
        informationType: '',
        informationCreateTime: '',
      },
      searParams: {},
      informationTypes: [
        { code: 'POLITICS', name: '政策法规' },
        { code: 'HOTSPOT', name: '热点新闻' },
        { code: 'INDUSTRY', name: '行业动态' },
      ],
      open: false,
      modalTitle: 'add',
      queryParamsAdd: {
        informationType: '', // 资讯类型
        informationName: '', // 资讯标题
        informationPublishPeople: '', // 资讯来源
        informationContent: '', // 资讯内容简介
        url: '',
        fileName: '',
      },
      fileLists: [],
      upload: {
        isUploading: false,
        headers: { Authorization: 'Bearer ' + getToken() },
        url: this.$store.state.global.fileUrl + '/file/uploadFile',
      },
      typeList: {
        政策法规: 'POLITICS',
        热点新闻: 'HOTSPOT',
        行业动态: 'INDUSTRY',
      },
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.contBoxHeight =
        document.getElementsByClassName('app-container')[0].clientHeight - this.$refs.queryForm.$el.clientHeight - 155;

      this.searParams = this.$store.state.lsfh.searParams;
    });
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      getNewsInfor(this.queryParams)
        .then(response => {
          this.tableList = response.rows;
          this.total = response.total;
          this.loading = false;
          if (this.total > 0) {
            this.contBoxHeight =
              document.getElementsByClassName('app-container')[0].clientHeight -
              this.$refs.queryForm.$el.clientHeight -
              155 -
              30;
          }
        })
        .catch(err => {
          this.loading = false;
          this.$message.error(err);
        });
    },
    netAddNewZx() {
      addNewsInfor(this.queryParamsAdd).then(res => {
        this.open = false;
        this.resetQuery();
        this.cancel();
        this.$message.success('新增成功');
      });
    },
    netEditZx() {
      editZx(this.queryParamsAdd).then(res => {
        this.open = false;
        this.resetQuery();
        this.cancel();
        this.$message.success('编辑成功');
      });
    },
    netRemoveZx(data) {
      removeZx(data).then(res => {
        this.$message.success(res.msg);
        this.resetQuery();
      });
    },
    handleAdd() {
      this.modalTitle = 'add';
      this.open = true;
    },
    handleEdit(row) {
      this.modalTitle = 'edit';
      this.open = true;
      this.queryParamsAdd = cloneDeep(row);
      this.queryParamsAdd.informationType = this.typeList[this.queryParamsAdd.informationType];
      this.fileLists = [
        {
          name: row.fileName,
          url: this.$store.state.global.fileUrl + row.url,
        },
      ];
    },
    handleRemove(row) {
      this.$modal
        .confirm('是否确认要删除？', '删除')
        .then(() => {
          this.netRemoveZx([row.id]);
        })
        .catch(() => {});
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    save() {
      // this.addNewXmRequest();
      this.$refs['queryFormAdd'].validate(valid => {
        if (valid) {
          this.modalTitle === 'add' ? this.netAddNewZx() : this.netEditZx();
        } else {
          return false;
        }
      });
    },
    cancel() {
      this.open = false;
      // this.resetForm('queryFormAdd');
      for (let key in this.queryParamsAdd) {
        this.$set(this.queryParamsAdd, key, '');
      }
      this.$refs.upload.clearFiles();
    },
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    handleFileSuccess(response, file, fileList) {
      if (response.code === 200) {
        this.upload.isUploading = false;
        this.queryParamsAdd.url = response.data.url;
        this.queryParamsAdd.fileName = file.name;
        // this.$refs.upload.clearFiles();
        // this.$message.success('上传成功');
      }
      // this.$message.error(response.msg);
    },
  },
};
</script>

<template>
  <div class="app-container">
    <div class="searBox" ref="searBox">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="资讯类型" prop="informationType">
          <el-select
            class="el-input_search"
            v-model="queryParams.informationType"
            placeholder="请选择"
            clearable
            size="small"
            style="width: 140px !important"
          >
            <el-option v-for="dict in informationTypes" :key="dict.code" :label="dict.name" :value="dict.code" />
          </el-select>
        </el-form-item>
        <el-form-item label="资讯标题" prop="informationName">
          <el-input
            v-filter-input
            style="width: 140px"
            v-model.trim="queryParams.informationName"
            placeholder="请输入"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="发布时间" prop="informationCreateTime">
          <el-date-picker
            v-model="queryParams.informationCreateTime"
            value-format="yyyy-MM-dd"
            size="small"
            type="date"
            placeholder="选择日期"
            align="left"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
          <el-button size="small" type="primary" plain @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="contBox">
      <div class="u-df-spb mb20">
        <div class="tableTit u-prim-text">资讯列表</div>
        <div>
          <el-button type="primary" plain icon="el-icon-plus" size="small" @click="handleAdd">新增资讯</el-button>
        </div>
      </div>

      <el-table v-loading="loading" :data="tableList" border class="khlbTable" :height="contBoxHeight">
        <!-- <el-table-column type="selection" width="50" align="center" /> -->
        <el-table-column type="index" width="55" label="序号" align="center" />
        <el-table-column label="资讯标题" align="center" prop="informationName" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            {{ scope.row.informationName | nullishCoalescing }}
          </template>
        </el-table-column>
        <el-table-column label="资讯类型" align="center" prop="informationType">
          <template slot-scope="scope">
            {{ scope.row.informationType | nullishCoalescing }}
          </template>
        </el-table-column>
        <el-table-column label="资讯来源" align="center" prop="informationPublishPeople">
          <template slot-scope="scope">
            {{ scope.row.informationPublishPeople | nullishCoalescing }}
          </template>
        </el-table-column>
        <el-table-column label="发布时间" align="center" prop="informationCreateTime">
          <template slot-scope="scope">
            {{ scope.row.informationCreateTime | nullishCoalescing }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-button class="lastTableBtn" size="mini" type="text" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button class="lastTableBtn" size="mini" type="text" @click="handleRemove(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <el-dialog :visible.sync="open" :before-close="cancel" center width="60%" append-to-body custom-class="zixun-modal">
      <div slot="title" class="dialog-title u-df-st">
        <span class="tit">{{ modalTitle === 'add' ? '新增' : '编辑' }}-</span>
        <span class="tit_con">资讯信息</span>
      </div>
      <el-form :model="queryParamsAdd" ref="queryFormAdd" label-position="left" label-width="120px">
        <el-form-item label="资讯类型" prop="informationType">
          <el-select
            class="el-input_search"
            v-model="queryParamsAdd.informationType"
            placeholder="请选择"
            clearable
            size="small"
            style="width: 300px !important"
          >
            <el-option v-for="dict in informationTypes" :key="dict.code" :label="dict.name" :value="dict.code" />
          </el-select>
        </el-form-item>
        <el-form-item label="资讯标题" prop="informationName">
          <el-input
            class="el-input_search"
            v-model="queryParamsAdd.informationName"
            placeholder="请输入"
            clearable
            size="small"
            style="width: 300px !important"
          />
        </el-form-item>
        <el-form-item label="资讯来源" prop="informationPublishPeople">
          <el-input
            class="el-input_search"
            v-model="queryParamsAdd.informationPublishPeople"
            placeholder="请输入"
            clearable
            size="small"
            style="width: 300px !important"
          />
        </el-form-item>
        <el-form-item label="资讯内容简介" prop="informationContent">
          <el-input
            v-model="queryParamsAdd.informationContent"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 10 }"
            placeholder="请输入资讯内容简介"
            style="width: 300px !important"
          />
        </el-form-item>
        <el-form-item label="文件上传" prop="url">
          <div class="u-df-st">
            <el-upload
              ref="upload"
              accept=".pdf"
              :limit="1"
              :headers="upload.headers"
              :action="upload.url"
              :disabled="upload.isUploading"
              :on-progress="handleFileUploadProgress"
              :on-success="handleFileSuccess"
              :fileList="fileLists"
            >
              <el-button type="primary" plain size="small" icon="el-icon-upload2" @click="">资讯上传</el-button>
            </el-upload>
            <div class="u-df-st u-ml30">
              <i class="el-icon-warning-outline u-fs14"></i>
              <span class="u-fs12" style="margin-left: 3px">上传资讯文章正文内容，PDF文件</span>
            </div>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.tableTit {
  font-size: 16px;
  font-family: Microsoft YaHei-Regular, Microsoft YaHei;
}
// .khlbTable {
//   ::v-deep .el-table__body-wrapper {
//     height: 330px;
//     overflow-y: auto;
//   }
// }
</style>
<style lang="scss">
.app-dark {
  .el-textarea__inner {
    background-color: rgba(0, 0, 0, 0.5);
    border: 1px solid #262f48;
  }
  .el-textarea__inner:hover {
    border-color: #09a9de;
  }
  .el-upload-list__item:hover {
    background: unset;
  }
}
</style>
